/**
 * note
 * @author Thot
 */

function MesureComponent()
{
	TransformableBitmap.apply(this);
	this._pointer = new Arrow();
	this._pointer.addEventListener("change", this, this._actualizeArrow);
	
	//dpe 55 - 12
	
	this.limits = new Array();
	this.limits.push(0); //A
	this.limits.push(67 / 454); // B
	this.limits.push(137 / 454 ); // C
	this.limits.push(201 / 454 ); // D
	this.limits.push(268 / 454 ); // E
	this.limits.push(335 / 454 ); // F
	this.limits.push(390 / 454 ); // G
	this.name = "MesureComponent";
	
	this._pointerPos = 0;
	this.addChild(this._pointer);
}

Tomahawk.registerClass( MesureComponent, "MesureComponent" );
Tomahawk.extend( "MesureComponent","TransformableBitmap" );

MesureComponent.RANGE_DPE					= [0,51,91,151,231,331,451];
MesureComponent.RANGE_GES					= [0,6,11,21,36,56,81];
MesureComponent.prototype.range				= [0,51,91,151,231,331,451];//default dpe
MesureComponent.prototype._pointer 			= null;
MesureComponent.prototype.limits 			= null;
MesureComponent.prototype._pointerPos 		= 0;

MesureComponent.prototype._actualizeArrow = function()
{
	var i = this.range.length;
	var value = this._pointer.getValue();
	
	while( --i > -1 )
	{
		if( value >= this.range[i] )
		{
			this._pointerPos = i;
			break;
		}
	}
	
	this.refresh();
};

MesureComponent.prototype._mouseHandler 	= function(event)
{
	if( event.type == tomahawk_ns.MouseEvent.DOUBLE_CLICK && event.target.isChildOf(this._pointer) )
	{
		this._pointer.select(true);
	}
	
	if(  event.type == tomahawk_ns.MouseEvent.MOUSE_DOWN && event.target == this._body )
	{
		this._pointer.select(false);
	}
	
	TransformableBitmap.prototype._mouseHandler.apply(this, [event] );
};

MesureComponent.prototype.refresh			= function()
{
	this.allowChangeImage = this.clipLeft = this.clipTop = this.clipRight = this.clipBottom = false;
	TransformableBitmap.prototype.refresh.apply(this);
	
	if( this._image == null )
		return;
	
	//this._pointer.setMask(this._region);
	var limit = this.limits[this._pointerPos];
	var naturalHeight = this._image.texture.rect[3];
	var naturalWidth = this._image.texture.rect[2];
	var ratioX = this._region.width / naturalWidth;
	var ratioY = this._region.height / naturalHeight;
	
	
	this._pointer.scaleX = ratioX;
	this._pointer.scaleY = ratioY;
	this._pointer.x = this._image.width - ( this._pointer.width * ratioX ) - 5;
	this._pointer.y = limit * this._image.height + this._image.y;
	
	
	this.addChild(this._pointer);
	//this.addChild(this._image);
};

MesureComponent.prototype.unserialize 		= function(obj)
{
	this.limits = obj.limits;
	this.range = obj.range;
	this._pointerPos = parseInt(obj.value);
	this._pointer.setValue( obj.realValue );
	TransformableBitmap.prototype.unserialize.apply(this,[obj]);
};

MesureComponent.prototype.serialize 		= function()
{
	var obj = TransformableBitmap.prototype.serialize.apply(this,[obj]);
	obj.type = "MesureComponent";
	obj.limits = this.limits;
	obj.range = this.range;
	obj.realValue = this._pointer.getValue();
	obj.realValue = ( isNaN( obj.realValue ) ) ? 0 : obj.realValue;
	obj.value = parseInt(this._pointerPos);
	return obj;
};





